/**
 * Copyright (C), 2020-2021
 * FileName: DecodeService
 * Author:   86189
 * Date:     2021/3/8 23:11
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.itheima.wallet.web.service;

import com.itheima.user.api.LoginUserApi;
import com.itheima.user.pojo.User;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 〈一句话功能简述〉<br> 
 * 〈jwt解密〉
 *
 * @author 86189
 * @create 2021/3/8
 * @since 1.0.0
 */
@Service
public class DecodeService {

    private String secret = "123456";

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Reference(version = "1.0.0")
    private LoginUserApi loginUserApi;


    public User queryUserByToken(String token) {
        try {
            // 通过token解析数据
            Map<String, Object> body = Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();

            Long id = Long.valueOf(body.get("id").toString());

            User user = loginUserApi.findById(id);
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
